import { defineConfig, loadEnv } from 'vite'
import vue from '@vitejs/plugin-vue'
import path, { resolve } from 'path'
import setupExtend from 'vite-plugin-vue-setup-extend'
import autoImport from 'unplugin-auto-import/vite'
import { viteMockServe } from 'vite-plugin-mock'
import { createHtmlPlugin } from 'vite-plugin-html'

const createAutoImport = function () {
    return autoImport({
        imports: ['vue', 'vue-router'],
        dts: false
    })
}

export default defineConfig(({ mode }) => {
    const env = loadEnv(mode, process.cwd())
    return {
        plugins: [
            vue(),
            createAutoImport(),
            setupExtend(),
            viteMockServe({
                mockPath: 'src/common/service/mock',
                enable: true
            }),
            createHtmlPlugin({
                inject: {
                    data: {
                        SYS_TITLE: env.VITE_APP_SYS_NAME
                    }
                }
            })
        ],
        resolve: {
            alias: {
                // 设置 路径
                '~': path.resolve(__dirname, './'),
                // 设置 别名
                '@': path.resolve(__dirname, './src')
            }
        },
        server: {
            port: 3300,
            host: true,
            open: false,
            hmr: true,
            proxy: {
                '/user/': {
                    target: 'http://10.70.1.192:81/user',
                    changeOrigin: true,
                    ws: true,
                    rewrite: (p) => p.replace(/^\/user/, '')
                }
            }
        },
        base: './',
        css: {
            preprocessorOptions: {
                scss: {
                    additionalData: '@use "./src/style/common.scss" as *;'
                }
            }
        }
    }
})
